﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// <summary>
/// Summary description for PobieraniePostow
/// </summary>
public class LoadData
{
    ForumDataContext dc;
    public LoadData(ForumDataContext d1)
	{
        dc = d1;
	}
    public Post[] ReturnPosts(int id_tematu)
    {
        Post[] posts;

        var p1 = from p in dc.Posties where p.id_temat == id_tematu select p;
        int wchichPost = 0;
        posts = new Post[p1.Count()];

        foreach (var rec in p1)
        {
            posts[wchichPost] = new Post();

            posts[wchichPost].Author = rec.id_uzytkownik.ToString();
            posts[wchichPost].Date = rec.czas.ToString();
            posts[wchichPost].Content = rec.tresc;

            var p2 = from p in dc.Uzytkownicies where p.id_uzytkownik == rec.id_uzytkownik select p.nick;

            foreach (var rec1 in p2) posts[wchichPost].Author = rec1.ToString();

            wchichPost++;
        }
        return posts;
    }

    public Topic[] ReturnTopics(int id_dzialu)
    {
        Topic[] topics;

        var p1 = from p in dc.Tematies where p.id_dzial == id_dzialu select p;
        int wchichPost = 0;
        topics = new Topic[p1.Count()];

        foreach (var rec in p1)
        {
            topics[wchichPost] = new Topic();

            topics[wchichPost].id = rec.id_temat;
            topics[wchichPost].Author = "";
            topics[wchichPost].Name = rec.nazwa;

            var p2 = (from p in dc.Posties where p.id_temat == rec.id_temat select p.czas);
            int last = p2.Count();
            p2 = p2.Take(last);

            foreach (var rec1 in p2) topics[wchichPost].Date = rec1.ToString();

            wchichPost++;
        }
        return topics;
    }
    public Thread[] ReturnThreads()
    {
        Thread[] threads;

        var p1 = from p in dc.Dzialies select p;
        int wchichPost = 0;
        threads = new Thread[p1.Count()];

        foreach (var rec in p1)
        {
            threads[wchichPost] = new Thread();

            threads[wchichPost].id = rec.id_dzial;
            threads[wchichPost].Name = rec.nazwa;

            wchichPost++;
        }
        return threads;
    }

    public string ReturnCurrentThread(int id)
    {
        string nazwa ="";

        var p1 = from p in dc.Dzialies where p.id_dzial == id select p;
        foreach (var rec in p1)
        {
            nazwa = rec.nazwa;
        }
        return nazwa;
    }

    public string ReturnCurrentTopic(int id)
    {
        string nazwa = "";

        var p1 = from p in dc.Tematies where p.id_temat == id select p;
        foreach (var rec in p1)
        {
            nazwa = rec.nazwa;
        }
        return nazwa;
    }
}